from django.contrib.auth.models import User
from django.db import models

from chat.models import Post


# Create your models here.
class Classes(models.Model):
    """
    数一 数二 数三
    """
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50, unique=True)

    def __str__(self):
        return self.name

class Label(models.Model):
    """
    对应的科目
    """
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=50, unique=False)
    classes = models.ForeignKey(Classes, on_delete=models.CASCADE, null=True)


class Courses(models.Model):
    title = models.CharField(max_length=30)  # 标题
    classes = models.ForeignKey(Classes, on_delete=models.NOT_PROVIDED, related_name='course_classes')
    teacher = models.CharField(max_length=30,null=True)  # 老师名称
    label = models.ForeignKey(Label, on_delete=models.NOT_PROVIDED, related_name='course_label')
    desc = models.CharField(max_length=60)  # 描述信息
    free = models.BooleanField(default=True)
    cover = models.TextField()  # 封面图 保存的是图片所在的地址
    video = models.TextField()  # 课程视频
    page_view = models.IntegerField(default=0)
    create_time = models.DateTimeField(auto_now_add=True)  # 创建时间  自动将创建时间保存并且不能修改
    update_time = models.DateTimeField(auto_now=True)  # 更新时间

    def __str__(self):
        return '%s %s'%(self.title, self.classes)

class CouCom(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    courses = models.ForeignKey(Courses, on_delete=models.CASCADE, related_name='courses_comments',null=True)
    # books = models.ForeignKey(Books, on_delete=models.CASCADE, related_name='books_comments',null=True)
    content = models.TextField()
    parent_comment = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='parent_comment_set')
    # reply_comment = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='reply_comment_set')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username}: {self.content[:20]}"

class Books(models.Model):
    title = models.CharField(max_length=30)  # 书名
    press = models.CharField(max_length=30)  # 出版社
    teacher = models.CharField(max_length=30)   # 老师名称
    desc = models.CharField(max_length=60)  # 简介
    cover = models.TextField()  # 封面图 保存的是图片所在的地址
    classes = models.ForeignKey(Classes, on_delete=models.NOT_PROVIDED, related_name='book_classes')  # 分类
    label = models.ForeignKey(Label, on_delete=models.NOT_PROVIDED,related_name='book_label')
    content = models.TextField()  # 内容
    page_view = models.IntegerField(default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title

class BookCom(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    books = models.ForeignKey(Books, on_delete=models.CASCADE, related_name='books_comments',null=True)
    content = models.TextField()
    parent_comment = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='parent_comment_set')
    created_at = models.DateTimeField(auto_now_add=True)

    class Meta:
        ordering = ['-created_at']

    def __str__(self):
        return f"{self.user.username}: {self.content[:20]}"

class Collections(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    books = models.ForeignKey(Books, on_delete=models.CASCADE, related_name='collections_books',null=True)
    courses = models.ForeignKey(Courses, on_delete=models.CASCADE, related_name='collections_courses',null=True)
    posts = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='collections_posts',null=True)
    create_time = models.DateTimeField(auto_now_add=True)

    class Meta:
        unique_together = [
            ('user', 'courses'),
            ('user', 'books'),
            ('user', 'posts')
        ]  # 防止重复收藏

class History(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    books = models.ForeignKey(Books, on_delete=models.CASCADE, related_name='history_books',null=True)
    courses = models.ForeignKey(Courses, on_delete=models.CASCADE, related_name='history_courses',null=True)
    posts = models.ForeignKey(Post, on_delete=models.CASCADE, related_name='history_posts',null=True)
    create_time = models.DateTimeField(auto_now_add=True)
    class Meta:
        unique_together = [
            ('user', 'books'),
            ('user', 'courses'),
            ('user', 'posts')
        ]

# class Articles(models.Model):
#     title = models.CharField(max_length=30)  # 标题
#     classes = models.ForeignKey(Classes,
#                                 on_delete=models.NOT_PROVIDED,related_name='article_classes')  # 当分类下还有文章，删除分类时不允许删除  related_name='a'为重定义管理器名称默认情况下不修改除非管理器与其他变量名称发生冲突
#     desc = models.CharField(max_length=60)  # 描述信息
#     content = models.TextField()  # 文章内容 TextField为文本字段不用设施长度 写多少是多少
#     cover = models.TextField()  # 封面图 保存的是图片所在的地址
#     user = models.ForeignKey(User, on_delete=models.CASCADE)
#     create_time = models.DateTimeField(auto_now_add=True)  # 创建时间  自动将创建时间保存并且不能修改
#     update_time = models.DateTimeField(auto_now=True)  # 更新时间
#
#     def __str__(self):
#         return '%s %s' % (self.title, self.classes)